package com.example.weddingapp.view.dialog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.DialogFragment;

import com.example.weddingapp.R;
import com.example.weddingapp.databinding.FragmentSelectMessageDialogBinding;

/**
 * @Date 2020/4/25 14:58
 * @Desc 提示框
 */

public class SelectMessageDialogFragment extends DialogFragment implements View.OnClickListener {
    private FragmentSelectMessageDialogBinding binding;
    private OnItemClickLinstener itemClickLinstener;

    public OnItemClickLinstener getItemClickLinstener() {
        return itemClickLinstener;
    }

    public void setItemClickLinstener(OnItemClickLinstener itemClickLinstener) {
        this.itemClickLinstener = itemClickLinstener;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cancel_btn:
                if (itemClickLinstener != null) {
                    itemClickLinstener.OnCancel();
                }
                dismiss();
                break;
            case R.id.confirm_btn:
                if (itemClickLinstener != null) {
                    itemClickLinstener.OnConfirm();
                }
                dismiss();
                break;
        }
    }

    public interface OnItemClickLinstener {
        void OnCancel();

        void OnConfirm();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil
                .inflate(inflater, R.layout.fragment_select_message_dialog, container, false);
        getDialog().getWindow().setBackgroundDrawableResource(R.color.transparent);
        Bundle bundle = getArguments();
        if (bundle != null) {
            binding.message.setText(getArguments().getString("title"));
            binding.confirmBtn.setText(getArguments().getString("confirmText"));
            if (getArguments().getString("cancelText") != null) {
                binding.cancelBtn.setText(getArguments().getString("cancelText"));
            }
        }

        binding.cancelBtn.setOnClickListener(this);
        binding.confirmBtn.setOnClickListener(this);

        return binding.getRoot();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Dialog);
    }


    /**
     * 修改提示内容和确认按钮的内容
     */
    public static SelectMessageDialogFragment newInstance(String title, String confirmText) {
        SelectMessageDialogFragment instance = new SelectMessageDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("confirmText", confirmText);
        instance.setArguments(bundle);
        return instance;
    }

    public static SelectMessageDialogFragment newInstance(String title, String cancelText, String confirmText) {
        SelectMessageDialogFragment instance = new SelectMessageDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("confirmText", confirmText);
        bundle.putString("cancelText", cancelText);
        instance.setArguments(bundle);
        return instance;
    }

    public static SelectMessageDialogFragment newInstance() {
        SelectMessageDialogFragment instance = new SelectMessageDialogFragment();
        return instance;
    }

}
